//
//  DetectorViewController.m
//  sign Detector
//
//  Created by Oaf Tobark on 5/14/13.
//  Copyright (c) 2013 Thoi Thai. All rights reserved.
//

#import "DetectorViewController.h"
#import "RecordingViewController.h"
#import "UIImage+OpenCV.h"
#import "SURFScanner.h"
#import "DataCenter.h"

#define AlertWelcome_Tag    101
#define ActionSheet_Require_Set_Sign    102

static DetectorViewController *_Share = nil;

@interface DetectorViewController () <UIAlertViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>{
    IBOutlet UISwitch *_theSwitch;
}

- (IBAction)lockApp:(id)sender;

//For test
- (IBAction)detected:(id)sender;

@end

@implementation DetectorViewController

+ (DetectorViewController*)shared{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_Share) {
            _Share = [[DetectorViewController alloc] initWithNibName:@"DetectorViewController" bundle:[NSBundle mainBundle]];
        }
    });
    
    return _Share;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Detecting...";
    self.navigationItem.hidesBackButton = YES;
    if ([[DataCenter shared] appLock]) {
        _theSwitch.on = YES;
    }else{
        _theSwitch.on = NO;
    }
    self.navigationItem.titleView = _theSwitch;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([[DataCenter shared] appLock]) {
        _theSwitch.on = YES;
    }else{
        _theSwitch.on = NO;
    }
    self.navigationItem.titleView = _theSwitch;
    if (![[DataCenter shared] standardImageSign]) {
#if TARGET_IPHONE_SIMULATOR
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerCameraCaptureModePhoto;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
#else
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerCameraDeviceFront;
        picker.delegate = self;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
#endif
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private Methods

- (IBAction)detected:(id)sender{
    [_theSwitch setOn:NO];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Hi Boss, nice to meet you" delegate:self cancelButtonTitle:@"CLose" otherButtonTitles:nil];
    alert.tag = AlertWelcome_Tag;
    [alert show];
}

- (IBAction)lockApp:(id)sender{
    if (_theSwitch.on == NO) {
        [[DataCenter shared] lock:^{
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"From Cameraroll", @"Take New Picture", nil];
            actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
            actionSheet.tag = ActionSheet_Require_Set_Sign;
            [actionSheet showInView:self.navigationController.view];
        }];
    }
}

#pragma mark - Public Methods

- (void)gotoRecordingScreen{
    [self.navigationController pushViewController:[RecordingViewController shared] animated:YES];
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (alertView.tag == AlertWelcome_Tag) {
        [self gotoRecordingScreen];
    }
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerCameraCaptureModePhoto;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
    }else if (buttonIndex == 1){
#if TARGET_IPHONE_SIMULATOR
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerCameraCaptureModePhoto;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
#else
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerCameraDeviceFront;
        picker.delegate = self;
        [self.navigationController presentViewController:picker animated:YES completion:^{
            
        }];
#endif
    }
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSData *imagedata = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage" ],1);
    UIImage *image = [[UIImage alloc] initWithData:imagedata];
    if ([[SURFScanner shared] compare2Image:image with:[[DataCenter shared] standardImageSign]]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Hi Boss, nice to meet you" delegate:self cancelButtonTitle:@"CLose" otherButtonTitles:nil];
        alert.tag = AlertWelcome_Tag;
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"You hack me ?" delegate:nil cancelButtonTitle:@"CLose" otherButtonTitles:nil];
        alert.tag = AlertWelcome_Tag;
        [alert show];
    }
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
}

@end
